<?php
/*
 * @Author: 哲学家乔布斯 <wuyangii@qq.com>
 * @Date: 2022-10-25 20:19:21
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-10-25 20:22:43
 * @Descripttion: 
 */


namespace app\common\model;

use think\Model;

class BusGoodsSku extends Base
{
    protected $insert = ['addtime'];

    //---------------自动完成----------------------
    protected function setAddtimeAttr()
    {
        return time();
    }
    public function coupon($money,$user_coupon_id){
        
        $goods_money = $money;$coupon_money = 0;
        $user_coupon = (new \app\common\model\UserCoupon())
                        ->where(['id' => $user_coupon_id,'state' => 0])
                        ->field(['id','title','money','bus_id','addtime','edntime','full_money','type','goods_id'])
                        ->append(['addtime_text','edntime_text'])
                        ->hidden(['addtime','edntime'])
                        ->find();
        switch (!empty($user_coupon['type'])) {
            case 3:
                if($money > $user_coupon['money']){
                    $goods_money = $money-$user_coupon['money'];
                    $coupon_money = $user_coupon['money'];
                }else{
                    $goods_money = $money;
                }
                break;
            case 2:
                if($money > $user_coupon['full_money'] && $user_coupon['money'] < $money){
                    $goods_money = $money-$user_coupon['money'];
                    $coupon_money = $user_coupon['money'];
                }else{
                    $goods_money = $money;
                }
                break;
            case 1:
                if($money > $user_coupon['money']){
                    $goods_money = $money-$user_coupon['money'];
                    $coupon_money = $user_coupon['money'];
                }else{
                    $goods_money = $money;
                }
                break;
        }
        return [
            'goods_money' => sprintf('%.2f',$goods_money),
            'coupon_money' => sprintf('%.2f',$coupon_money),
            'money_cost' => sprintf('%.2f',$money),
        ];
    }


    /**
     * 获取图片数组
     *
     * @param [type] $value
     * @param [type] $data
     * @return void
     */
    public function getCoverImageTextAttr($value, $data)
    {
        return cdnurl($data['cover_image'],true);
    }
    public function getMoneyAttr($value,$data){
        return (new \app\common\model\BusGoodsSku())->where(['is_default' => 1])->value('money');
    }
    public function getSkuAttr($value,$data){
        return (new \app\common\model\BusGoodsSku())->field(['id','sku_title','money'])->where(['goods_id' => $data['id']])->select();
    }
    public function getBusUserNameAttr($value,$data){
        return (new \app\common\model\Bus())->where(['id' => $data['bus_id']])->value('user_name');
    }
    public function getAgentUserNameAttr($value,$data){
        return (new \app\common\model\Agent())->where(['id' => $data['agent_id']])->value('usename');
    }
    public function getCateTitleAttr($value,$data){
        return (new \app\common\model\Classify())->where(['id' => $data['cate_id']])->value('name');
    }
    public function getBrandTitleAttr($value,$data){
        return (new \app\common\model\GoodsBrand())->where(['id' => $data['brand_id']])->value('title');
    }
    public function getCarTitleAttr($value,$data){
        return (new \app\common\model\GoodsCar())->where(['id' => $data['car_id']])->value('title');
    }
}
